{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 01_split_dataset\n",
    "#\n",
    "# created by LuYF-Lemon-love <luyanfeng_nlp@qq.com> on February 25, 2023\n",
    "# updated by LuYF-Lemon-love <luyanfeng_nlp@qq.com> on February 25, 2023\n",
    "#\n",
    "# 该脚本展示了如何划分 DRKG 数据集.\n",
    "#\n",
    "# 需要的包:\n",
    "#          numpy\n",
    "#          pandas\n",
    "#\n",
    "# 需要的文件:\n",
    "#          ../../data/drkg/drkg.tsv\n",
    "#\n",
    "# 源教程链接: https://github.com/gnn4dr/DRKG/blob/master/embedding_analysis/Train_embeddings.ipynb"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Split DRKG to train/valid/test set\n",
    "\n",
    "这个 notebook 展示了如何划分数据集."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 导入需要的库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "np.random.seed(42)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Prepare train/valid/test set\n",
    "\n",
    "在训练之前，我们需要以 9:0.5:0.5 的比例将原始 drkg 分成训练集/验证集/测试集."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "drkg_file = '../../data/drkg/drkg.tsv'\n",
    "\n",
    "df = pd.read_csv(drkg_file, sep=\"\\t\", header=None)\n",
    "triples = df.values.tolist()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们得到 5,874,261 个三元组, 现在我们将它们划分到三个文件中."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5874261"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "num_triples = len(triples)\n",
    "num_triples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "!mkdir -p ./dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "len(train_set):\t5286834\n",
      "len(vaild_set):\t293713\n",
      "len(test_set):\t293714\n"
     ]
    }
   ],
   "source": [
    "seed = np.arange(num_triples)\n",
    "np.random.shuffle(seed)\n",
    "\n",
    "train_cnt = int(num_triples * 0.9)\n",
    "valid_cnt = int(num_triples * 0.05)\n",
    "train_seed = seed[:train_cnt].tolist()\n",
    "valid_seed = seed[train_cnt:train_cnt + valid_cnt].tolist()\n",
    "test_seed = seed[train_cnt + valid_cnt:].tolist()\n",
    "\n",
    "with open(\"dataset/drkg_train.tsv\", 'w+') as f:\n",
    "    for idx in train_seed:\n",
    "        f.writelines(\"{}\\t{}\\t{}\\n\".format(triples[idx][0], triples[idx][1], triples[idx][2]))\n",
    "        \n",
    "with open(\"dataset/drkg_valid.tsv\", 'w+') as f:\n",
    "    for idx in valid_seed:\n",
    "        f.writelines(\"{}\\t{}\\t{}\\n\".format(triples[idx][0], triples[idx][1], triples[idx][2]))\n",
    "\n",
    "with open(\"dataset/drkg_test.tsv\", 'w+') as f:\n",
    "    for idx in test_seed:\n",
    "        f.writelines(\"{}\\t{}\\t{}\\n\".format(triples[idx][0], triples[idx][1], triples[idx][2]))\n",
    "\n",
    "print(f\"len(train_set):\\t{len(train_seed)}\\nlen(vaild_set):\\t{len(valid_seed)}\\nlen(test_set):\\t{len(test_seed)}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
